package com.example.node.strategy;

import com.example.ProcessFlowContext;
import com.example.domain.Product;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@LiteflowComponent(id = "free", name = "执行满减策略")
@Component
@Slf4j
public class Free extends NodeComponent {
    @Override
    public void process() throws Exception {
        ProcessFlowContext context = this.getContextBean(ProcessFlowContext.class);
        BigDecimal amount = new BigDecimal(0);
        for (Product product : context.getProductList()) {
            amount = amount.add(product.getPrice());
        }
        // 满200减30
        if (amount.intValue() >= 200) {
            amount = amount.subtract(new BigDecimal(30));
        }
        log.info("满减后商品总价：{}", amount.floatValue());
        context.getStrategyMap().put("FREE", amount);
    }
}
